<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    template="/layout/template.xhtml">

<ui:define name="body">

    <h:form id="usuario" styleClass="edit">

        <rich:panel>
            <f:facet name="header">#{usuarioHome.managed ? 'Alterar' : 'Cadastro de'} Usuário</f:facet>

			<s:div styleClass="tableControl" rendered="#{s:hasRole('Subgrupo') and empty from}">
        		<s:link view="/resources/UsuarioList.xhtml"
		                    id="list"
		           propagation="none">
		            <h:graphicImage url="/img/list.png"/>
		            #{' '}
		            <h:outputLabel value="Listagem"/>
		        </s:link>
		    </s:div>
			
			<s:decorate id="nomeField" template="/layout/edit.xhtml">
                <ui:define name="label">Nome</ui:define>
                <h:inputText id="nome" value="#{usuarioHome.instance.nome}" required="true"/>
            </s:decorate>
            
            <s:decorate id="emailField" template="/layout/edit.xhtml">
                <ui:define name="label">E-mail</ui:define>
                <h:inputText id="email" value="#{usuarioHome.instance.email}" required="true">
                	<a:support event="onblur" reRender="emailField" bypassUpdates="true" ajaxSingle="true"/>
                </h:inputText>
            </s:decorate>
            
            <s:decorate id="statusField" template="/layout/edit.xhtml">
				<ui:define name="label">Status</ui:define>
            	<h:selectOneRadio id="status" value="#{usuarioHome.instance.status}" styleClass="radio" required="true">
                    <f:selectItem itemLabel="Ativo" itemValue="Ativo"/>
                    <f:selectItem itemLabel="Inativo" itemValue="Inativo"/>
                </h:selectOneRadio>
            </s:decorate>
            
            <s:decorate id="senhaField" template="/layout/edit.xhtml">
                <ui:define name="label">Senha</ui:define>
                <h:inputSecret id="senha" value="#{usuarioHome.instance.senha}" required="#{!usuarioHome.managed}"/>
            </s:decorate>
            
            <s:decorate id="confirmarSenhaField" template="/layout/edit.xhtml">
                <ui:define name="label">Confirmar Senha</ui:define>
                <h:inputSecret id="confirmarSenha" value="#{usuarioHome.instance.confirmarSenha}" required="#{!usuarioHome.managed}"/>
            </s:decorate>

            <div style="clear:both">
                <span class="required">*</span>
                campos obrigatórios 
            </div>

        </rich:panel>

        <div class="actionButtons">

            <h:commandButton id="save"
                          value="Salvar"
                         action="#{usuarioHome.persist}"
                       disabled="#{!usuarioHome.wired}"
                       rendered="#{!usuarioHome.managed}"/>

            <h:commandButton id="update"
                          value="Salvar"
                         action="#{usuarioHome.update}"
                       rendered="#{usuarioHome.managed}"/>

            <h:commandButton id="delete"
                          value="Deletar"
                         action="#{usuarioHome.remove}"
                      immediate="true"
                       rendered="#{usuarioHome.managed}"/>

            <s:button id="cancelEdit"
                   value="Cancelar"
             propagation="end"
                    view="/resources/Usuario.xhtml"
                rendered="#{usuarioHome.managed}"/>

            <s:button id="cancelAdd"
                   value="Cancelar"
             propagation="end"
                    view="/resources/#{empty usuarioFrom ? 'UsuarioList' : usuarioFrom}.xhtml"
                rendered="#{!usuarioHome.managed}"/>

        </div>
    </h:form>

<rich:tabPanel switchType="ajax">

    <rich:tab label="Categoria">
        <h:form styleClass="association" id="categoriaChildren">

            <h:outputText value="Não existe nenhuma Categoria cadastrada."
                       rendered="#{empty usuarioHome.categoria}"/>

            <rich:dataTable value="#{usuarioHome.categoria}"
                           var="_categoria"
                      rendered="#{not empty usuarioHome.categoria}"
                    rowClasses="odd,even"
                          rows="20"
                            id="categoriaTable">
                
                <rich:column sortBy="#{_categoria.nome}">
                    <f:facet name="header">Nome</f:facet>
                    <h:outputText value="#{_categoria.nome}"/>
                </rich:column>
                <rich:column sortBy="#{_categoria.descricao}">
                    <f:facet name="header">Descrição</f:facet>
                    <h:outputText value="#{_categoria.descricao}"/>
                </rich:column>
            </rich:dataTable>
            
            <br clear="left"/>
            
            <rich:datascroller for="categoriaTable" renderIfSinglePage="false" />

        </h:form>

        <f:subview rendered="#{usuarioHome.managed}" id="categoria">
        <div class="actionButtons">
            <s:button id="addcategoria"
                   value="Nova Categoria"
                    view="/resources/CategoriaEdit.xhtml"
             propagation="none">
                 <f:param name="usuarioId"
                         value="#{usuarioHome.instance.id}"/>
                 <f:param name="categoriaFrom" value="Usuario"/>
            </s:button>
        </div>
        </f:subview>
    </rich:tab>

    <rich:tab label="Subgrupo">
    <div class="association" id="subGrupoParent">

        <h:outputText value="Não existe nenhum Subgrupo associado."
                   rendered="#{usuarioHome.instance.subGrupo == null}"/>

        <rich:dataTable var="_subGrupo"
                   value="#{usuarioHome.instance.subGrupo}"
                rendered="#{usuarioHome.instance.subGrupo != null}"
              rowClasses="odd,even"
                      id="subGrupoTable">
            
            <h:column>
                <f:facet name="header">Nome</f:facet>
                <h:outputText value="#{_subGrupo.nome}"/>
            </h:column>
            <h:column>
                <f:facet name="header">E-mail</f:facet>
                <h:outputText value="#{_subGrupo.email}"/>
            </h:column>
            <h:column>
                <f:facet name="header">Descrição</f:facet>
                <h:outputText value="#{_subGrupo.descricao}"/>
            </h:column>
            <h:column>
                <f:facet name="header">Data de Cadastro</f:facet>
                <h:outputText value="#{_subGrupo.dataCadastro.time}"/>
            </h:column>
            <h:column>
                <f:facet name="header">Status</f:facet>
                <h:outputText value="#{_subGrupo.status}"/>
            </h:column>
        </rich:dataTable>

        <div class="actionButtons">
            <s:button id="#{usuarioHome.instance.subGrupo != null ? 'changeParent' : 'selectParent'}" value="#{usuarioHome.instance.subGrupo != null ? 'Trocar' : 'Selecionar'} Subgrupo"
                       view="/resources/SubGrupoList.xhtml"
                       rendered="#{s:hasRole('Grupo')}">
                <f:param name="from" value="UsuarioEdit"/>
            </s:button>
        </div>

    </div>
    </rich:tab>
</rich:tabPanel>
</ui:define>

</ui:composition>
